package cn.tangsihai.pay.controller;

import cn.tangsihai.basic.exception.MyException;
import cn.tangsihai.basic.util.AjaxResult;
import cn.tangsihai.pay.dto.AlipayNotifyDto;
import cn.tangsihai.pay.service.IAlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pay")
public class AlipayController {
    @Autowired
    private IAlipayService alipayService;

    @PostMapping("/alipay/{orderSn}")
    public AjaxResult alipay(@PathVariable("orderSn") String orderSn){
        try {
            String returnHtml = alipayService.alipay(orderSn);
            return AjaxResult.me().setResultObj(returnHtml);
        } catch (MyException e) {
            return AjaxResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }

    /**
     * 支付结果处理，异步回调
     * @param notifyDto
     * @return
     */
    @PostMapping("/calback")
    public AjaxResult calback(AlipayNotifyDto notifyDto){
        try {
            String calback = alipayService.calback(notifyDto);
            return AjaxResult.me().setResultObj(calback);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }
}
